package cn.udrm.water.base.v1.waterdistribute.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.udrm.water.base.v1.commons.models.PageModel;
import cn.udrm.water.base.v1.waterdistribute.dto.WaterGateDto;
import cn.udrm.water.base.v1.waterdistribute.dto.WaterGateDtoMapper;
import cn.udrm.water.base.v1.waterdistribute.dto.WaterGateVendorDto;
import cn.udrm.water.base.v1.waterdistribute.dto.WaterGateVendorDtoMapper;
import cn.udrm.water.base.v1.waterdistribute.entities.WaterGate;
import cn.udrm.water.base.v1.waterdistribute.entities.WaterGateVendor;
import cn.udrm.water.base.v1.waterdistribute.repository.WaterGateVendorRepository;

@Service
@Transactional(readOnly=true)
public class WaterGateVendorService {
	@Autowired
	private WaterGateVendorRepository gateVendorRepository;
	@Autowired
	private WaterGateVendorDtoMapper gateVendorDtoMapper;
	@Autowired
	private WaterGateService waterGateService;
	@Autowired
	private WaterGateDtoMapper waterGateDtoMapper;

	/**
	 * 分页条件查询
	 * @param pageNumberStr
	 * @param pageSizeStr
	 * @param waterGateSearch
	 * @param sortField
	 * @param sortType
	 * @return
	 */
	public PageModel<WaterGateVendorDto> getPageByConditions(String pageNumberStr, String pageSizeStr, final WaterGateVendorDto waterGateVendorSearch
			, String sortField, String sortType) {
		int number = 0,
			size = 0;
		try {
			number = Integer.parseInt(pageNumberStr);
		} catch (Exception e) {}
		try {
			size = Integer.parseInt(pageSizeStr);
		} catch (Exception e) {}
		size = size == 0 ? 10 : size;
		Sort sort = null;
		if (!StringUtils.isEmpty(sortField)) {
			try {
				sort = new Sort(StringUtils.isEmpty(sortType) ? Direction.ASC : (sortType.equals("ASC")) ? Direction.ASC : Direction.DESC, sortField);
			} catch (Exception e) {}
		} else {
			sort = new Sort(Direction.ASC, "createDate");
		}
		Pageable pageable = PageRequest.of(number, size, sort);
		Specification<WaterGateVendor> specification = new Specification<WaterGateVendor>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<WaterGateVendor> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				
				if (predicates.size() == 0) {
					return null;
				}
				Predicate predicateArr[] = new Predicate[predicates.size()];
				return cb.and(predicates.toArray(predicateArr));
			}
		};
		Page<WaterGateVendor> page = gateVendorRepository.findAll(specification, pageable);
		List<WaterGateVendor> content = page.getContent();
		List<WaterGateVendorDto> dtos = gateVendorDtoMapper.from(content);
		PageModel<WaterGateVendorDto> pageModel = new PageModel<WaterGateVendorDto>(page.getSize(), page.getNumber(), page.getTotalElements(), dtos);
		return pageModel;
	}
	
	public WaterGateVendorDto getWithWaterGates(String id) {
		WaterGateVendor findAllWithWaterGates = gateVendorRepository.findAllWithWaterGates(id);
		if (findAllWithWaterGates == null) {
			return null;
		}
		WaterGateVendorDto vendorDto = gateVendorDtoMapper.from(findAllWithWaterGates);
		List<WaterGate> waterGates = findAllWithWaterGates.getWaterGates();
		if (waterGates != null && waterGates.size() != 0) {
			List<WaterGateDto> list = waterGateDtoMapper.from(waterGates);
			vendorDto.setWaterGateDtos(list);
		}
		return vendorDto;
	}
	
	public WaterGateVendorDto getAllById(String id) {
		WaterGateVendor waterGateVendor = gateVendorRepository.findAllById(id);
		WaterGateVendorDto waterGateVendorDto = gateVendorDtoMapper.from(waterGateVendor);
		return waterGateVendorDto;
	}
	
	public WaterGateVendorDto getSelfById(String id) {
		WaterGateVendor findSelfById = gateVendorRepository.findSelfById(id);
		WaterGateVendorDto from = gateVendorDtoMapper.from(findSelfById);
		return from;
	}
	
	@Transactional(readOnly=false)
	public WaterGateVendorDto save(WaterGateVendorDto waterGateVendorDto) {
		WaterGateVendor waterGateVendor = gateVendorDtoMapper.fromWaterGateVendorDto(waterGateVendorDto);
		waterGateVendor.setId(null);
		waterGateVendor.setCreateDate(new Date());
		waterGateVendor = gateVendorRepository.saveAndFlush(waterGateVendor);
		WaterGateVendorDto dto = gateVendorDtoMapper.from(waterGateVendor);
		return dto;
	}
	@Transactional(readOnly=false)
	public WaterGateVendorDto update(WaterGateVendorDto waterGateVendorDto) {
		WaterGateVendor waterGateVendor = gateVendorDtoMapper.fromWaterGateVendorDto(waterGateVendorDto);
		waterGateVendor = gateVendorRepository.saveAndFlush(waterGateVendor);
		WaterGateVendorDto dto = gateVendorDtoMapper.from(waterGateVendor);
		return dto;
	}
	
	@Transactional(readOnly=false)
	public void deleteAllSelf(String[] ids) {
		if (ids == null || ids.length == 0) {
			return;
		}
		for (String id : ids) {
			gateVendorRepository.deleteById(id);
		}
	}
	
	@Transactional(readOnly=false)
	public void deleteAll(String[] ids) {
		if (ids == null || ids.length == 0) {
			return;
		}
		List<WaterGateVendor> waterGateVendors = new ArrayList<WaterGateVendor>();
		for (String id : ids) {
			waterGateVendors.add(new WaterGateVendor(id));
		}
		this.deleteAll(waterGateVendors);
	}
	
	@Transactional(readOnly=false)
	public void deleteAll(Iterable<WaterGateVendor> iterable) {
		if (iterable == null || iterable.iterator() == null) {
			return;
		}
		Iterator<WaterGateVendor> iterator = iterable.iterator();
		while (iterator.hasNext()) {
			WaterGateVendor waterGateVendor = iterator.next();
			WaterGateVendor gateVendor = gateVendorRepository.findAllById(waterGateVendor.getId());
			List<WaterGate> waterGates = gateVendor.getWaterGates();
			waterGateService.deleteAll(waterGates);
			gateVendorRepository.delete(waterGateVendor);
		}
	}
}
